﻿using System.Linq;
using ACC.Data.Line;
using System.Collections.Generic;
using System;
using ACC.Data.Comm;
using System.Threading;
using ACC.Util.Log;
using ACC.Data;
using System.Data;
using ACC.Util.Common;
using System.Text;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Client;
using ACC.Device.Common;

namespace ACC.Device.Client
{
    public class ClientDevice : ClientCommonDevice
    {
        public ClientDevice(StnRunningData stnData, ACCDriver driver)
            : base(stnData, driver)
        {
        }

        public override int SendClientConfig(Entity ternimalCfg)
        {
            if (ternimalCfg == null)
            {
                ternimalCfg = new ClientConfig();
            }

            return _TerminalDriver.SendAccCmd(ACCCmd.ClientConfig, ternimalCfg);
        }

        public override void LoadClientConfig()
        {
            _ClientCfg = new ClientConfig();

            (_ClientCfg as ClientConfig).Line = _stnData.StnCfg.LineCfg.Line;
            (_ClientCfg as ClientConfig).OP = _stnData.StnCfg.OPCfg.OP;
            (_ClientCfg as ClientConfig).STN = _stnData.StnCfg.Name;
            (_ClientCfg as ClientConfig).Description = _stnData.StnCfg.Desc;
            (_ClientCfg as ClientConfig).PartTypeId = _stnData.PartNo == null ? 0 : (int)_stnData.PartNo.BomCfg.PartTypeID;
            (_ClientCfg as ClientConfig).OperatorLoginRequired = _stnData.StnCfg.OprLogon;
            (_ClientCfg as ClientConfig).IsFirstOP = _stnData.IsFirstStn || (_stnData.Route != null && _stnData.Route.Status == RoutingSetpCfg.RoutingCfgStatus.StandalongSubStation);

            if (!string.IsNullOrEmpty(_stnData.Operator))
            {
                (_ClientCfg as ClientConfig).OperatorId = _stnData.Operator;

                string sql = string.Format("SELECT NAME FROM ACC_OPERATOR WHERE LINE = '{0}' AND OPERATORID='{1}'", _lineData.Line, _stnData.Operator);
                DataTable dt = DataManager.DBHelper.Query(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    (_ClientCfg as ClientConfig).Name = dt.Rows[0]["Name"].ToString(); ;
                }
            }

            LineShiftCfg shift = _lineData.RunningData.GetCurrentShift();
            if (shift != null)
            {
                (_ClientCfg as ClientConfig).ShiftCfg = new ClientShiftCfg()
                {
                    FirstTime = shift.FirstTime,
                    Hours = shift.Hours,
                    ShiftName = shift.ShiftName,
                    ShiftOrder = shift.ShiftOrder,
                    ShiftStart = shift.ShiftStart
                };
            }

            string[] param = _stnData.StnCfg.Device == null ? null : _stnData.StnCfg.Device.Param.Split(';');
            if (param != null && param.Length > 2)
                (_ClientCfg as ClientConfig).ServerIP = param[2];
            if (param != null && param.Length > 3)
                (_ClientCfg as ClientConfig).ServerPort = int.Parse(param[3]);
        }

        public override ACCCode SendModelChangeRequest(ACCDevice.PartTypeDataResult result)
        {
            if (result.PartNo != null)
            {
                ClientPartType partType = new ClientPartType()
                {
                    PartTypeId = (int)result.PartNo.BomCfg.PartTypeID,
                    Sequence = (int)result.PartNo.BomCfg.Sequence,
                    PartNo = result.PartNo.BomCfg.PartNo,
                    Description = result.PartNo.BomCfg.Desc
                };

                if (_TerminalDriver.SendAccCmd(ACCCmd.ClientModelChange, partType) < 0)
                    return ACCCode.ACCInternalError;
            }

            return ACCCode.ACCOK;
        }
    }
}
